/**
 * Created by li on 2017/1/28.
 */
//地图类
function Map(array) {
    this.array = array;
}

Map.prototype = {
    constructor: Map,
    rePaint: function () {
        for (var i = 0; i < 20; i++) {
            for (var j = 0; j < 10; j++) {
                if (this.array[i][j] == 1) {
                    this.createCeil(j, i);
                } else {
                    this.disappearDiv(j, i);
                }
            }
        }
    },
    createCeil: function (x, y) {
        var id = 10 * y + x;
        var img = document.getElementById(id);
        if (img == null) {
            img = document.createElement('img');
            img.src = './resource/images/ceil.png';
            img.style.width = global.ceilWidth + 'px';
            img.style.height = global.ceilHeight + 'px';
            img.style.position = 'absolute';
            img.style.left = x * global.ceilWidth + 'px';
            img.id = id
            //img.style.top = y*global.ceilHeight+'px';
            img.style.top = 570 - y * global.ceilHeight + 'px';
            var main = document.getElementsByClassName('game-main-panel')[0];
            main.appendChild(img);
        } else {
            img.style.display = 'block';
        }

    },
    //每一行达到数值10，消除次行,数值上消除
    disappear: function () {
        for (var i = 0; i < 20; i++) {
            var result = 0;
            for (var j = 0; j < 10; j++) {
                result += this.array[i][j];
            }
            if (result == 10) {
                for (var j = 0; j < 10; j++) {
                    this.array[i][j] = 0;
                }
                //加分
                //消除
                this.rePaint();
                //下移动,
                this.landfall();
                //再次检测
                this.disappear();
            }
        }
        this.rePaint();
    },
    disappearDiv: function (x, y) {
        var id = 10 * y + x;
        var img = document.getElementById(id);
        if (img !== null) {
            img.style.display = 'none';
        }
    },
    //line为行号，0-19行
    landfall: function () {
        for (var i = 0; i < 20; i++) {
            for (var j = 0; j < 10; j++) {
                if (this.array[i][j] == 1) {
                    if (i - 1 >= 0) {
                        if (this.array[i - 1][j] == 0) {
                            this.array[i][j] = 0;
                            this.array[i - 1][j] = 1;
                            this.landfall();
                        }
                    }
                }
            }
        }
    },
    //x,(y)是四格的坐标，fourGrid是2维4*4数组
    //y设置为19,因为添加到最上层高度19,不是fourGrid.y的值，返回并更新一个fourGrid.y的值
    addFourGrid: function (fourGrid) {

        var x = fourGrid.x,
            xx = fourGrid.xx,
            yy = fourGrid.yy
        xLen = fourGrid.x + 4;

        if (xLen > 10) {
            alert('error');
            return
        }

        var y = 19;
        fourGrid.y = 19;

        //var yLen=20;

        for (var x = fourGrid.x; x < xLen; x++) {

            if (x >= 0 && x <= 9 && y >= 0) {
                this.array[y][x] = fourGrid.fourGridArr[yy][xx] || this.array[y][x];
            }

            // this.array[y][x] = fourGrid.fourGridArr[yy][xx] || this.array[y][x];
            // if (this.array[y][x] == 1 || this.array[y][x + 1] == 1 || this.array[y][x + 2] == 1 || this.array[y][x + 3] == 1) {
            //     this.array[y][x] = fourGrid.fourGridArr[yy][xx] || this.array[y][x];
            // } else {
            //     this.array[y][x] = fourGrid.fourGridArr[yy][xx]
            // }
            xx++;
        }


        //return 一个值，代表四格的最下方到达哪一行
        this.rePaint();
        global.isFalling = true;
        return fourGrid;
    },
    //对一个四格进行下一秒的操作
    nextSecond: function (fourGrid) {
        var y = fourGrid.y - 1,
            x = fourGrid.x,
            xx = fourGrid.xx,
            yy = fourGrid.yy;
        var xLen = x + 4;
        var yLen;
        if (y + 4 > 20) {
            yLen = 20;
        } else {
            yLen = y + 4;

        }


        this.nextSecondClean(fourGrid);

        //下一秒，fourGrid.y-1；
        if (fourGrid.y > 0) {
            fourGrid.y -= 1;
        } else {

        }

        for (y; y < yLen; y++) {
            var xx = fourGrid.xx;
            for (var x = fourGrid.x; x < xLen; x++) {

                if (x >= 0 && x <= 9 && y >= 0) {
                    this.array[y][x] = fourGrid.fourGridArr[yy][xx] || this.array[y][x];
                }

                // this.array[y][x] = fourGrid.fourGridArr[yy][xx] || this.array[y][x];

                // if (this.array[y][x] == 1 || this.array[y][x + 1] == 1 || this.array[y][x + 2] == 1 || this.array[y][x + 3] == 1) {
                //     this.array[y][x] = fourGrid.fourGridArr[yy][xx] || this.array[y][x];
                // } else {
                //     this.array[y][x] = fourGrid.fourGridArr[yy][xx]
                // }
                xx++;
            }
            yy--;
        }


        this.rePaint();
        return fourGrid;

    },
    //
    nextSecondClean: function (fourGrid) {

        //这个变量传递的问题，思考
        var y = fourGrid.y;
        x = fourGrid.x,
            xx = fourGrid.xx,
            yy = fourGrid.yy;
        var xLen = x + 4;
        var yLen;
        if (y + 4 > 20) {
            yLen = 20;
        } else {
            yLen = y + 4;

        }


        for (y; y < yLen; y++) {
            var xx = fourGrid.xx;
            for (var x = fourGrid.x; x < xLen; x++) {
                this.array[y][x] = 0;
                xx++;
            }
            yy--;

        }
        this.rePaint();
    },
    //重绘地图，参数为一个四格,isClean,是否消除，有时候是添加，有时候是消除,默认不消除
    rePaintWithfourGrid: function (fourGrid, isClean) {
        var isClean = isClean || false;
        var y = fourGrid.y,
            x = fourGrid.x,
            xx = fourGrid.xx,
            yy = fourGrid.yy;

        var xLen = x + 4;
        var yLen;
        if (y + 4 > 20) {
            yLen = 20;
        } else {
            yLen = y + 4;

        }
        if (isClean) {
            for (y; y < yLen; y++) {
                var xx = fourGrid.xx;

                for (var x = fourGrid.x; x < xLen; x++) {

                    if (x >= 0 && y >= 0) {
                        if (fourGrid.fourGridArr[yy][xx] == 1) {
                            this.array[y][x] = 0;
                        }

                    }

                    xx++;
                }
                yy--;
            }
        } else {
            for (y; y < yLen; y++) {
                var xx = fourGrid.xx;

                for (var x = fourGrid.x; x < xLen; x++) {
                    // debugger;
                    // if(fourGrid.fourGridArr[yy][xx]==1){

                    //                     console.log("y,x", y, x);
                    // console.log(this.array[y][x]);

                    // if (this.array[y][x] == 1 || this.array[y][x + 1] == 1 || this.array[y][x + 2] == 1 || this.array[y][x + 3] == 1) {
                    //     this.array[y][x] = fourGrid.fourGridArr[yy][xx] || this.array[y][x];
                    // } else {
                    //     this.array[y][x] = fourGrid.fourGridArr[yy][xx]
                    // }


                    // }
                    // console.log(y)
                    // console.log(this.array[y][x])

                    if (x >= 0 && x <= 9 && y >= 0) {
                        this.array[y][x] = fourGrid.fourGridArr[yy][xx] || this.array[y][x];
                    }

// try {
//     this.array[y][x] = fourGrid.fourGridArr[yy][xx] || this.array[y][x];
// }catch (err){
//     this.array[y][x] = fourGrid.fourGridArr[yy][xx] || this.array[y][x];
// }

                    // if (fourGrid.fourGridArr[yy][xx] == 1 && this.array[y][x] == 0) {
                    //     this.array[y][x] = 1
                    // } else {
                    //     this.array[y][x] = 0
                    // }

                    xx++;
                    // debugger;
                }
                yy--;
            }
            // console.log(this.array);
        }

        this.rePaint();
    },
    canIMove(fourGrid, direction) {
        var self = this;
        // var result = ""
        var flag = [];
        var y = fourGrid.y,
            x = fourGrid.x,
            xx = fourGrid.xx,
            yy = fourGrid.yy;

        var xLen = x + 4;
        var yLen;
        if (y + 4 > 20) {
            yLen = 20;
        } else {
            yLen = y + 4;

        }

        var minX = 0, maxX = 0;
        var currentMaxY, currentMinY;

        var minY = [], currentMinX;
        var firstLoop2 = true;
        for (y; y < yLen; y++) {
            var xx = fourGrid.xx;
            for (var x = fourGrid.x; x < xLen; x++) {

                if (fourGrid.fourGridArr[yy][xx] == 1) {

                    if (firstLoop2) {
                        minX = x;
                        maxX = x;
                        currentMinY = y
                        currentMaxY = y


                        currentMinX = x;
                        firstLoop2 = false;
                    }

                    if (minX > x) {
                        minX = x;
                        currentMinY = y;
                    }
                    if (maxX < x) {
                        maxX = x;
                        currentMaxY = y;
                    }


                }
                xx++;
            }
            yy--;
        }

        var y = fourGrid.y,
            x = fourGrid.x,
            xx = fourGrid.xx,
            yy = fourGrid.yy;
        var firstLoop = true;
        for (y; y < yLen; y++) {
            var xx = fourGrid.xx;
            for (var x = fourGrid.x; x < xLen; x++) {
                if (fourGrid.fourGridArr[yy][xx] == 1) {

                    if (direction == "left") {
                        if (x > 0) {
                            if (x = minX) {
                                if (this.array[currentMinY][minX - 1] != 1) {
                                    flag.push(true)
                                } else {
                                    flag.push(false)
                                }
                            } else {
                                flag.push(true)
                            }
                        } else {
                            flag.push(false)
                        }
                    }

                    if (direction == "right") {

                        if (x <= 9) {
                            if (x = maxX) {
                                if (this.array[currentMaxY][maxX + 1] != 1) {
                                    flag.push(true)
                                } else {
                                    flag.push(false)
                                }
                            } else {
                                flag.push(true);
                            }

                        } else {
                            flag.push(false)
                        }
                    }
                    if (direction == "down") {

// fourGrid.minY
//                         console.log(yy)
                        // console.log(xx)
                        console.log('------')
                        // console.log(fourGrid.x);
                        console.log(fourGrid.y);


                        fourGrid.minY.map(v => {

                            if (v.y == yy && v.x == xx) {

                                if (self.array[fourGrid.y - (3 - v.y)][fourGrid.x + (3 - v.x)] != 1) {
                                    flag.push(true);
                                } else {
                                    global.isFalling = false
                                }
                                //
                                // if (this.array[y - 1][x] != 1) {
                                //     flag.push(true);
                                // } else {
                                //     global.isFalling = false
                                //     return false
                                // }
                            }
                        })

                        // this.array[y - 1][x] != 1
                        // console.log(fourGrid.minY)

                        // console.log(y)
                        // console.log(x)

                        // if (firstLoop) {
                        //
                        //
                        //
                        //     firstLoop = false;
                        // }else{
                        //
                        // }

                        // for(var k=0;k<3,k++){
                        //
                        // }
                        // if (firstLoop) {
                        //     if (y > 0) {
                        //         if (this.array[y - 1][x] != 1 && this.array[y - 1][x+1] != 1 &&this.array[y - 1][x+2] != 1 &&this.array[y - 1][x+3] != 1) {
                        //             flag.push(true);
                        //         } else {
                        //             global.isFalling = false
                        //             // return "isFalling"
                        //             return false
                        //         }
                        //
                        //     } else {
                        //         flag.push(false)
                        //
                        //         // fourGrid.isFalling="2";
                        //
                        //     }
                        //     firstLoop = false;
                        // } else {
                        //     if (y > 0) {
                        //         flag.push(true);
                        //     } else {
                        //         flag.push(false)
                        //     }
                        // }


                    }

                }

                xx++;

            }
            yy--;

        }
        // console.log(flag);
        if (flag.indexOf(false) != -1) {
            // result = true
            return false
        } else {
            // result = false
            return true
        }
        // console.log(this.array);
    }
}